package com.yurendao.shop.forum.service.impl;

import com.yurendao.shop.forum.dao.NoteCateMapper;
import com.yurendao.shop.forum.domain.Ncategory;
import com.yurendao.shop.forum.domain.Note;
import com.yurendao.shop.forum.domain.NoteCate;
import com.yurendao.shop.forum.domain.NoteCateExample;
import com.yurendao.shop.forum.exception.NoteException;
import com.yurendao.shop.forum.service.NoteCateService;
import com.yurendao.shop.pager.domain.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kong on 16/2/17.
 */
@Service
public class NoteCateServiceImpl implements NoteCateService {

    @Autowired
    private NoteCateMapper noteCateMapper;

    @Override
    public Page<Note> findNoteByCate(Integer ncid, String orderByClause, Integer pageOffset) throws NoteException{
        NoteCateExample noteCateExample = new NoteCateExample();
        noteCateExample.setNcid(ncid);
        noteCateExample.setOrderByClause(orderByClause);
        int pc = Page.getPc(pageOffset);
        int ps = Page.getPs(pageOffset);
        noteCateExample.setPc(pc);
        noteCateExample.setPs(ps);
        List<Note> notes = noteCateMapper.getNoteByCate(noteCateExample);
        int totalcount = noteCateMapper.countNoteByCate(ncid);
        Page<Note> notePage = new Page<Note>(notes,pageOffset,totalcount);
        return notePage;
    }

    @Override
    public void insertCate(String ncname, Integer npid) throws NoteException{
        NoteCate noteCate = new NoteCate();
        noteCate.setNcname(ncname);
        noteCate.setNpid(npid);
        noteCateMapper.insertNoteCate(noteCate);
    }

    @Override
    public List<Ncategory> getAllCategory() throws NoteException {
        List<Ncategory> ncategories = new ArrayList<Ncategory>();
        List<NoteCate> noteCates = noteCateMapper.getAllNoteCate();
        Map<Integer,List<Ncategory>> map = new HashMap<Integer,List<Ncategory>>();
        for (int i = 0; i < noteCates.size(); i++) {
            if(noteCates.get(i).getNpid()==null){
                Ncategory ncategory = new Ncategory();
                ncategory.setNcid(noteCates.get(i).getNcid());
                ncategory.setNcname(noteCates.get(i).getNcname());
                List<Ncategory> childs = new ArrayList<Ncategory>();
                ncategory.setChilds(childs);
                ncategories.add(ncategory);
                map.put(noteCates.get(i).getNcid(),childs);
            }
        }
        for (int i = 0; i < noteCates.size(); i++) {
            if(noteCates.get(i).getNpid()!=null){
                List<Ncategory> childs = map.get(noteCates.get(i).getNpid());
                Ncategory ncategory = new Ncategory();
                ncategory.setNcid(noteCates.get(i).getNcid());
                ncategory.setNcname(noteCates.get(i).getNcname());
                childs.add(ncategory);
            }
        }

        return ncategories;
    }

    @Override
    public void updateCate(NoteCate noteCate) {
        noteCateMapper.updateNoteCate(noteCate);
    }

    @Override
    public void deleteNoteCate(Integer ncid) throws NoteException {
        noteCateMapper.deleteNoteCate(ncid);
    }


}
